<?php
/* SVN FILE: $Id: scriptures_controller.php 71 2007-12-10 00:21:14Z joe.vasquez $ */
/**
 * scriptures controller
 * @filesource
 * @author			Joe Vasquez <joe.vasquez@gmail.com>
 * @copyright		Copyright (c) 2007, Joe Vasquez
 * @package			redlettr
 * @subpackage		scriptures
 * @since			0.1A
 * @version			0.1A  $Rev: 71 $
 * @lastmodified	$Date: 2007-12-10 00:21:14 +0000 (Mon, 10 Dec 2007) $
 * @license			http://www.gnu.org/licenses/gpl.html GNU General Public License
 */
 
class ScripturesController extends AppController {

	var $name = 'Scriptures';
	var $uses = array('Scripture','User');
	
	function index() {
		$this->Menu->createMenu();
		$this->Menu->addItem(array('Word Search' => '/scriptures/search'));		
		
		$this->pageTitle = 'Scriptures';
	}

	function search()
	{
		if(func_num_args() > 0)
		{
			$this->data['Scripture'] = $this->__fixArgs(func_get_args());
		} elseif($this->data) {	
			$this->data['Scripture'] = $this->__fixArgs($this->data['Scripture']);
		}
		
		if($this->data)
		{
			$mrClean = new Sanitize();
			$this->data['Scripture'] = $mrClean->paranoid(
				$this->data['Scripture'],
				array(
					' ','.','-','\'','"'
				)
			);
			$this->Scripture->data = $this->data['Scripture'];
			$this->set('results',$this->Scripture->doQuery());
		}
		
		$this->helpers[] = 'Esv';
		$this->set('scripts',array('jquery.form.js'));
		
		$this->Breadcrumbs->addCrumb('Scriptures');
		$this->Breadcrumbs->addCrumb('Search','/scriptures/search');
	}
	
	function __fixArgs($args)
	{
		$fixedArgs = $this->Scripture->options;
		if(count($args) == 1)
		{
			if(preg_match('/=/',$args[0]) == 0)
			{
				$args = array('q' => $args[0]);
			}
		}
		foreach($args as $argKey => $argVal)
		{
			if(preg_match('/=/',$argVal) == 1)
			{
				$argVal = strtolower($argVal);
				$tmp = explode('=',$argVal);
				if(array_key_exists($tmp[0],$fixedArgs))
				{
					$fixedArgs[$tmp[0]] = $tmp[1];
				}
			} else {
				$fixedArgs[$argKey] = $argVal;
			}
		}
		foreach($this->Scripture->overwriteOptions as $overwriteKey =>$overwriteOption)
		{
			$fixedArgs[$overwriteKey] = $overwriteOption;
		}
		return $fixedArgs;
	}
}
?>